/**
 * MIT License
 * 
 * Copyright (c) 2021 CaiDongyu
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.axe.spring.cloud.configer;

import java.util.Set;

import org.axe.helper.ioc.ClassHelper;
import org.axe.util.AxeProperties;
import org.mysql4axe.Mysql;
import org.mysql4axe.helper.MysqlConfigHelper;
import org.mysql4axe.spring.cloud.annotation.EnableM4A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @author CaiDdongyu
 * @date 2021-6-25
 */
public class Mysql4AxeSpringConfigInitializer  implements AxeComponentInitInterface{
	 private final Logger LOGGER = LoggerFactory.getLogger(Mysql4AxeSpringConfigInitializer.class);

	 @Override
	 public void init() {
	  		try {
	  			Set<Class<?>> enableM4AClassSize = ClassHelper.getClassSetByAnnotation(EnableM4A.class);
	  	    	if(enableM4AClassSize.size() > 1) {
	  	    		for(Class<?> cls:enableM4AClassSize) {
	  	    			LOGGER.error(cls.getSimpleName());
	  	    		}
	  	    		throw new Exception("find more than one class has annotation @EnableM4A");
	  	    	}else if(enableM4AClassSize.size() == 0){
	  	    		return;
	  	    	}
	  	    	
	  	    	//当找到一个@EnableM4A的时候，开始初始化
	  			MysqlConfigHelper.setConfig(AxeProperties.getPROPERTIES());
	  			Mysql.config();

				LOGGER.info("mysql4axe init success!");
	  		} catch (Exception e) {
	  			LOGGER.error(e.getMessage(), e);
	  			System.exit(0);
	  		}
	  }
	 
	 
}